<?php
/**
 * 広告コントローラー
 *
 * @package  app
 * @extends  Controller
 */
class Controller_Api_Category extends Controller_Apibase {
	/**
	 * コンストラクタ
	 *
	 * @access  public
	 * @return  Response
	 */
	public function before() {
		// 広告系は認証いらない
		$this->auth = FALSE;
		
		parent::before();
		$get = Input::get();
	}

	/**
	 * get all category 
	 */
        public function  action_listcategories()
        {   
            $json = array();
            $post = Input::get();
            $limit = isset($post['limit'])?$post['limit']:0;
            
            $query = DB::select('id',DB::expr('ct_name AS name'),DB::expr('ct_desc AS description'),'create_date');
            $query ->from('bk_catagories');
            $query->order_by('create_date','desc');
          
            if($limit!==0)
            {
                $query->limit($limit);
            }
            $data = $query->execute()->as_array();
            $json['status']= 0;
            $json['data'] = $data;
            return Response::forge(json_encode($json), D_HTTP_UPDATE);
            
        }
        public function action_gettracks()
        {
            $json = array();
            $get = Input::get();
            $limit =0;
            $cat_id = 0;
           if(isset($get['limit']))
           {        
                   
                   $limit= $get['limit'];
           }
           if(isset($get['cat_id']))
           {
               
                $cat_id = $get['cat_id'];
           }
            
          //  $cat_id = isset($post('cat_id'))? $post('cat_id') : 0;
            
            $query = DB::select('bk_audio_story.id',DB::expr('title AS name'),DB::expr('category_id AS cat_id'),DB::expr('ct_name AS cat_alias'),DB::expr('full_link AS link'),DB::expr('description AS description'),DB::expr('avatar_link AS image'),'bk_audio_story.create_date');
            $query ->from('bk_audio_story');
            $query -> join('bk_catagories', 'LEFT') -> on('bk_catagories.id', '=', 'bk_audio_story.category_id');
            $query->order_by('bk_audio_story.create_date','desc');
            if($cat_id !==0)
            {
                $query->where('bk_audio_story.category_id',$cat_id);
            }
            if($limit!==0)
            {
                $query->limit($limit);
            }
         
            
            $data = $query->execute()->as_array();
            $json['status']= 0;
            $json['data'] = $data;
            return Response::forge(json_encode($json), D_HTTP_UPDATE);
        }
        public function  action_gettrackinfo()
        {
            $json = array();
            $post = Input::get();
            $limit = isset($post['limit'])?$post['limit']:0;
            $id = isset($post['id'])?$post['id']:0;
            if($id==0)
            {
                $json['status']= 1;
                $json['data']=array();
                return Response::forge(json_encode($json), D_HTTP_UPDATE);
                
            }
            $query = DB::select('bk_audio_story.id',DB::expr('title AS name'),DB::expr('category_id AS cat_id'),DB::expr('ct_name AS cat_alias'),'bk_audio_story.create_date');
            $query ->from('bk_audio_story');
            $query -> join('bk_catagories', 'LEFT') -> on('bk_catagories.id', '=', 'bk_audio_story.category_id');
            if($limit!==0)
            {
                $query->limit($limit);
            }
            $query->where('bk_audio_story.id',$post['id']);
            $data = $query->execute()->as_array();
            $json['status']= 0;
            $json['data'] = $data;
            return Response::forge(json_encode($json), D_HTTP_UPDATE);
            
            
        }
        
}
